package team.scau.laboratory_repair.service;

import com.querydsl.core.QueryResults;
import com.querydsl.core.types.dsl.EntityPathBase;
import com.querydsl.jpa.impl.JPAQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.scau.laboratory_repair.common.base.BaseService;
import team.scau.laboratory_repair.common.constant.ConstantRole;
import team.scau.laboratory_repair.common.page.MyPage;
import team.scau.laboratory_repair.common.utils.Md5Utils;
import team.scau.laboratory_repair.modules.po.QWechatUser;
import team.scau.laboratory_repair.modules.po.WechatUser;
import team.scau.laboratory_repair.modules.repository.WechatUserRepository;

import java.util.ArrayList;
import java.util.List;

@Service
public class WechatUserService extends BaseService<WechatUser, WechatUserRepository> {

    @Autowired
    private WechatUserRepository wechatUserRepository;

    private QWechatUser qWechatUser = QWechatUser.wechatUser;

    @Override
    public WechatUserRepository getRepository() {
        return wechatUserRepository;
    }

    @Override
    public EntityPathBase<WechatUser> getQBase() {
        return qWechatUser;
    }

    public QueryResults<WechatUser> listOrByExample(WechatUser example, MyPage page) {
        JPAQuery<WechatUser> jpaQuery = queryFactory.select(qWechatUser).from(qWechatUser);
        if (example != null && StringUtils.isNotBlank(example.getPhoneNumber())) {
            jpaQuery = jpaQuery.where(qWechatUser.phoneNumber.eq(example.getPhoneNumber()));
        }
        jpaQuery = jpaQuery.where(qWechatUser.valid.eq(true));
        if (page != null) {
            jpaQuery = jpaQuery.orderBy(page.getSort()).offset(page.getPage()).limit(page.getSize());
        }
        return jpaQuery.fetchResults();

    }

    public List<WechatUser> listStaffByIds(List<Long> ids){
        if(ids == null || ids.size() == 0){
            return new ArrayList<>();
        }
        JPAQuery<WechatUser> jpaQuery = queryFactory.select(qWechatUser).from(qWechatUser)
                .where(qWechatUser.id.in(ids))
                .where(qWechatUser.valid.eq(true));
        return jpaQuery.fetch();
    }

    public List<WechatUser> listManager(){
        JPAQuery<WechatUser> jpaQuery = queryFactory.select(qWechatUser).from(qWechatUser)
                .where(qWechatUser.valid.eq(true))
                .where(qWechatUser.userType.eq(Math.toIntExact(ConstantRole.ROLE_MANAGER)));
        return jpaQuery.fetch();
    }

    public WechatUser findByPhoneNumber(String phoneNumber) throws Exception {
        JPAQuery<WechatUser> jpaQuery = queryFactory.select(qWechatUser).from(qWechatUser).where(qWechatUser.valid.eq(true));
        if (StringUtils.isNotBlank(phoneNumber)) {
            jpaQuery = jpaQuery.where(qWechatUser.phoneNumber.eq(phoneNumber));
        }
        return jpaQuery.fetchOne();
    }

    public WechatUser findByOpenId(String openId) throws Exception{
        JPAQuery<WechatUser> jpaQuery = queryFactory.select(qWechatUser).from(qWechatUser).where(qWechatUser.valid.eq(true));
        if (StringUtils.isNotBlank(openId)) {
            jpaQuery = jpaQuery.where(qWechatUser.openid.eq(openId));
        }
        return jpaQuery.fetchOne();
    }

}
